package com.kyh.util.plist;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;

import com.kyh.util.qiniucloud.QiniuCloudUtil;

public class PlistUtil {
	public static void main(String[] args) throws IOException {
//		File temp;
//		try {
//			temp = File.createTempFile("myTempFile", ".txt");
//
//			System.out.println("Temp file created : " + temp.getAbsolutePath());
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
		String absolutePath = createPlist("https://files.cardezu.com/downLoadApp/3.3.7/Payload.ipa");
		uploadTempFileToQiniuAndDelete(absolutePath);
	}

	/**
	 * 创建plist临时文件
	 * @param ipaFileUrl ipa安装包的完整路径
	 * @return 返回临时文件所在的完全路径
	 * @throws IOException
	 */
	public static String createPlist(String ipaFileUrl) throws IOException {
		System.out.println("==========开始创建plist文件");
		File temp = File.createTempFile("tempFile", ".plist");
		
		String plist = "<plist version=\"1.0\">\n"
				+ "  <dict>\n"
				+ "      <key>items</key>\n"
				+ "      <array>\n"
				+ "        <dict>\n"
				+ "           <key>assets</key>\n"
				+ "           <array>\n"
				+ "             <dict>\n"
				+ "               <key>kind</key>\n"
				+ "               <string>software-package</string>\n"
				+ "               <key>url</key>\n"
				+ "               <string><![CDATA[" + ipaFileUrl + "]]></string>\n"
				+ "             </dict>\n"
				+ "             <dict>\n"
				+ "               <key>kind</key>\n"
				+ "               <string>display-image</string>\n"
				+ "               <key>needs-shine</key>\n"
				+ "               <integer>0</integer>\n"
				+ "               <key>url</key>\n"
				+ "               <string><![CDATA[https://files.cardezu.com/downLoadApp/kalian.png]]></string>\n"
				+ "             </dict>\n"
				+ "             <dict>\n"
				+ "               <key>kind</key>\n"
				+ "               <string>full-size-image</string>\n"
				+ "               <key>needs-shine</key>\n"
				+ "               <true/>\n"
				+ "               <key>url</key>\n"
				+ "               <string><![CDATA[https://files.cardezu.com/downLoadApp/kalian.png]]></string>\n"
				+ "             </dict>\n"
				+ "           </array>\n"
				+ "           <key>metadata</key>\n"
				+ "           <dict>\n"
				+ "             <key>bundle-identifier</key>\n"
				+ "             <string>com.yingzhi8.kaezu</string>\n"
				+ "             <key>bundle-version</key>\n"
				+ "             <string><![CDATA[3.3.7]]></string>\n"
				+ "             <key>kind</key>\n"
				+ "             <string>software</string>\n"
				+ "             <key>title</key>\n"
				+ "             <string><![CDATA[卡链优品]]></string>\n"
				+ "           </dict>\n"
				+ "        </dict>\n"
				+ "      </array>\n"
				+ "  </dict>\n"
				+ "</plist>";
		try {
			FileOutputStream output = new FileOutputStream(temp);
			OutputStreamWriter writer;
			writer = new OutputStreamWriter(output, "UTF-8");
			writer.write(plist);
			writer.close();
			output.close();
		} catch (Exception e) {
			System.err.println("==========创建plist文件异常：" + e.getMessage());
		}
		
		return temp.getAbsolutePath();
	}
	
	public static String uploadTempFileToQiniuAndDelete(String absolutePath) {
		String fileName = null;
		File file = new File(absolutePath);
		
		try {
			InputStream fileInputStream = new FileInputStream(file);
			String sourceFilename = "xxx.plist";
			fileName = new QiniuCloudUtil().upload(fileInputStream, sourceFilename);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		file.delete();
		
		return fileName;
	}
}